package com.cas.netty;

import com.cas.activemq.QueueSender;
import com.cas.constant.HJ212;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;

@ChannelHandler.Sharable
@Component
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    private static final Logger logger = LoggerFactory.getLogger("data-access");
    private static final ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    @Autowired
    private QueueSender queueSender;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
        String localIp = localAddress.getAddress().getHostAddress();
        int localPort = localAddress.getPort();
        InetSocketAddress clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = clientAddress.getAddress().getHostAddress();
        int clientPort = clientAddress.getPort();
        //获取连接通道的唯一标识
        ChannelId channelId = ctx.channel().id();

        Channel channel = ctx.channel();
        channelGroup.add(channel);

        logger.info("[TcpServer-(IP:{})-(Port:{})-(ChannelId:{})]-<新建连接>-[TcpClient-(IP:{})-(Port:{})];[TcpServer]-(ChannelSize:{})", localIp, localPort, channelId, clientIp, clientPort, channelGroup.size());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //获取服务端、客户端连接的IP和PORT
        InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
        String localIp = localAddress.getAddress().getHostAddress();
        int localPort = localAddress.getPort();
        InetSocketAddress clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = clientAddress.getAddress().getHostAddress();
        int clientPort = clientAddress.getPort();
        //获取连接通道的唯一标识
        ChannelId channelId = ctx.channel().id();

        Channel channel = ctx.channel();
        channelGroup.remove(channel);

        logger.info("[TcpServer-(IP:{})-(Port:{})-(ChannelId:{})]-<断开连接>-[TcpClient-(IP:{})-(Port:{})];[TcpServer]-(ChannelSize:{}))", localIp, localPort, channelId, clientIp, clientPort, channelGroup.size());
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //获取服务端、客户端连接的IP和PORT
        InetSocketAddress localAddress = (InetSocketAddress) ctx.channel().localAddress();
        String localIp = localAddress.getAddress().getHostAddress();
        int localPort = localAddress.getPort();
        InetSocketAddress clientAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        String clientIp = clientAddress.getAddress().getHostAddress();
        int clientPort = clientAddress.getPort();
        //获取此连接通道的唯一标识
        ChannelId channelId = ctx.channel().id();

        //服务端接收到的数据
        String s = msg.toString();

        // 如果数据以##开头， 则是HJ212协议的数据
        if (s.startsWith("##")) {
            // 解析出要发送的消息队列名称
            String mqName = HJ212.defaultMqName;
            // 发送消息到消息队列
            queueSender.SendMsg(mqName, s);
        }

        //群发消息
        logger.info("[TcpServer-(IP:{})-(Port:{})-(ChannelId:{})]-<Receive>->[TcpClient-(IP:{})-(Port:{})]-<Data>-[[{}]]", localIp, localPort, channelId, clientIp, clientPort, s);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}
